package com.example.smartcampus;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    EditText user, password;
    Button login;
    ImageButton register;

    SQLiteDatabase dbRead;
    String username1, password1;

    @SuppressLint({"MissingInflatedId", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getView();
//        加载数据库
        loadDatabase();
//        注册按钮
        registerButton();
//设计一个登录按钮，账号和密码和SQLite里面的一一比较
        login();
    }

    private void login() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username1 = user.getText().toString().trim();
                password1 = password.getText().toString().trim();
                if (validateLogin(username1, password1)) {
                    Intent intent = new Intent(MainActivity.this, News.class);
                    intent.putExtra("username", username1);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private boolean validateLogin(String username1, String password1) {
        @SuppressLint("Recycle")
        Cursor cursor = dbRead.query("information", new String[]{"user", "password"}, "user = ? AND password = ?",
                new String[]{username1, password1},
                null, null, null);

        if (cursor.moveToFirst()) {
            // 如果找到了匹配的用户名和密码
            @SuppressLint("Range") String dbUsername = cursor.getString(cursor.getColumnIndex("user"));
            @SuppressLint("Range") String dbPassword = cursor.getString(cursor.getColumnIndex("password"));

            if (dbUsername.equals(username1) && dbPassword.equals(password1)) { // 注意：在实际应用中，应比较哈希值而不是明文密码
                cursor.close();
                return true;
            }
        }

        cursor.close();
        Toast.makeText(MainActivity.this, "登录失败，请检查用户名和密码", Toast.LENGTH_SHORT).show();
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void registerButton() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Register.class);
                startActivity(intent);
            }
        });
    }

    private void loadDatabase() {
        try {
            Mydata dbOpenHelper = new Mydata(getApplicationContext(), "inform_database.db", null, 1);
            dbRead = dbOpenHelper.getReadableDatabase();
        } catch (Exception e) {
            Log.d("database", "loadDatabase: 数据库加载失败");
        }
    }


    private void getView() {
        user = findViewById(R.id.user);
        password = findViewById(R.id.password);
        login = findViewById(R.id.button_login);
        register = findViewById(R.id.button_register);


    }
}